<?php


namespace app\admin\controller;


use app\model\ModelUserAccountCost;
use app\service\UserAccountService;
use easyadmin\app\columns\lists\ListImage;
use easyadmin\app\columns\lists\ListText;
use easyadmin\app\libs\ListField;
use easyadmin\app\libs\Page;
use easyadmin\app\libs\PageList;
use think\db\Query;
use think\response\Json;

/**
 * 用户提现审核
 * Class ReviewUserCost
 * @package app\admin\controller
 */
class ReviewUserCost extends Admin
{
    protected $pageName = "用户提现审核";
    protected $tableName = 'user_account_cost';
    protected $disabledAction = ['add', 'show', 'edit', 'delete'];

    protected function configListJoin(Page $page, Query $query, string $alias)
    {
        $query->join('user', 'user.id='. $alias .'.user_id');
        $query->join('user_account_card', 'user_account_card.id='. $alias .'.my_card_id');
    }

    protected function configListWhere(Page $page, Query $query, $alias)
    {
        $query->where([
            'user_account_cost.status' => 1 ,//提现申请的状态: 1 待审核; 2 驳回; 3 待付款; 4 已付款;
        ]);
    }

    protected function configListField(ListField $list)
    {
        $list
            ->addField('id', 'ID', ListText::class)
            ->addField('user_id', '用户ID', ListText::class)
            ->addField('user.real_name', '用户名', ListText::class)
            ->addField('number', '流水号', ListText::class)
            ->addField('amount', '提现金额', ListText::class)
            ->addField('user_account_card.income_account_type', '收款类型', ListText::class)
            ->addField('user_account_card.income_account', '收款账号', ListText::class)
            ->addField('user_account_card.income_account_name', '收款人姓名', ListText::class)
            ->addField('user_account_card.income_account_code_url', '收款码', ListImage::class)
            ;
    }

    protected function configList(PageList $page)
    {
        $page
            ->addAction('审核通过', 'reviewCashOut?type=3',[
                'confirm' => '确定审核通过',
            ])
            ->addAction('审核驳回', 'reviewCashOut?type=2',[
                'class' => 'layui-btn layui-btn-warm',
                'confirm' => '确定审核驳回',
            ])
        ;
        $page->setOrderBy(["id" => "desc"]);
        $page->setTemplate("list:list");
    }

    public function reviewCashOut() : Json
    {
        $input = input();
        $id = trim($input['id']);
        $type = trim($input['type']);
        $checkUserId = $this->getLoginUserId();
        $remark = '';
        $costModel = new ModelUserAccountCost();
        $userId = $costModel->find($id)['user_id'];
        if(empty($userId)) return $this->error('查不到用户id');
        $res = UserAccountService::instance()->checkUserAccountCostById($userId, $id, $type, $checkUserId, $remark);
        if($res['status']){
            return $this->success($res);
        }else{
            return $this->error($res);
        }
    }


}